 //
//  MYXWithdrawalsViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/12/1.
//  Copyright © 2017年 hbjApple. All rights reserved.
//  提现界面

#import "MYXWithdrawalsViewController.h"
#import "CYPasswordView.h"
#import "MYXMYXWithdrawalsCell.h"
#import "MYXbalanceCell.h"
#import "KGModal.h"
#import "CashShowView.h"
static NSString *const MYXMYXWithdrawalsCellIdentifier = @"MYXMYXWithdrawalsCell";
static NSString *const MYXbalanceCellIdentifier = @"MYXbalanceCell";
@interface MYXWithdrawalsViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>{
    MYXMYXWithdrawalsCell *MYXMYXWithdrawalsCe ;
    MYXbalanceCell *MYXbalanceCe;
    CGFloat lastOriginalY;
}
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong) CashShowView *cashView;
@property (nonatomic, strong) CYPasswordView *passwordView;

@end

@implementation MYXWithdrawalsViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =MYYColorFromRGB(0xf2f2f2);
    [self createUI];
}
#pragma mark - UI

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -SafeAreaTopHeight, WIDTH, HEIGHT+20) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = MYYColorFromRGB(0xf2f2f2);
        _tableView.dataSource = self;
        _tableView.delegate = self;  
    }
    return _tableView;
}
- (CashShowView *)cashView{
    if (_cashView == nil) {
        _cashView = [CashShowView loadNibCashView];
    }
    return _cashView;
}
#pragma mark - request

- (void)loadNewData
{
    [self.tableView.mj_header beginRefreshing];
    [self.dataArr removeAllObjects];
    //[self mypublish:self.pageStart];
    
}

#pragma mark - tableview datasource and delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==1) {
        return 1;
    }
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf)
    if (indexPath.section==0) {
        MYXMYXWithdrawalsCe = [tableView dequeueReusableCellWithIdentifier:MYXMYXWithdrawalsCellIdentifier forIndexPath:indexPath];
        MYXMYXWithdrawalsCe.selectionStyle = UITableViewCellSelectionStyleNone;
        MYXMYXWithdrawalsCe.backgroundColor = MYYColorFromRGB(0xf2f2f2);
        MYXMYXWithdrawalsCe.allCashMoney = self.restMoney;
        return MYXMYXWithdrawalsCe;
    }
    MYXbalanceCe = [tableView dequeueReusableCellWithIdentifier:MYXbalanceCellIdentifier forIndexPath:indexPath];
    MYXbalanceCe.selectionStyle = UITableViewCellSelectionStyleNone;
    MYXbalanceCe.backgroundColor = MYYColorFromRGB(0xf2f2f2);
    [MYXbalanceCe.BalanceButton setTitle:@"提现" forState:UIControlStateNormal];
    __weak MYXMYXWithdrawalsCell * weakCell = MYXMYXWithdrawalsCe;
    MYXbalanceCe.BalanceB = ^(){
        //FIXME: - 提现
        if (weakCell.moneyTF.text.floatValue > 0.1) {
            [KGModal sharedInstance].closeButtonType = KGModalCloseButtonTypeNone;
            [KGModal sharedInstance].modalBackgroundColor = [UIColor whiteColor];
            weakSelf.cashView.moneyCout = weakCell.moneyTF.text;
            [[KGModal sharedInstance] showWithContentView:weakSelf.cashView];
            weakSelf.cashView.finishCashBlock = ^(NSString *account){
                [weakSelf payPasswordWithMoney:weakCell.moneyTF.text payid:account];
            };
           
        }
       
    };
    
    return MYXbalanceCe;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 272+108-50;
    }
    
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (void)createUI{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [self.tableView registerNib:[UINib nibWithNibName:MYXMYXWithdrawalsCellIdentifier bundle:nil] forCellReuseIdentifier:MYXMYXWithdrawalsCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:MYXbalanceCellIdentifier bundle:nil] forCellReuseIdentifier:MYXbalanceCellIdentifier];
}
- (void)payPasswordWithMoney:(NSString *)cashMoney payid:(NSString *)payid {
    /** 注册取消按钮点击的通知 */
    [CYNotificationCenter addObserver:self selector:@selector(cancel) name:CYPasswordViewCancleButtonClickNotification object:nil];
    [CYNotificationCenter addObserver:self selector:@selector(forgetPWD) name:CYPasswordViewForgetPWDButtonClickNotification object:nil];
    __weak typeof(self) weakself  = self;
    self.passwordView = [[CYPasswordView alloc] init];
    self.passwordView.title = @"输入交易密码";
    self.passwordView.loadingText = @"提交中...";
    [self.passwordView showInView:[KGModal sharedInstance].window];
    __weak MYXMYXWithdrawalsCell * weakCell = MYXMYXWithdrawalsCe;

    self.passwordView.finish = ^(NSString *password) {
        [weakself.passwordView hideKeyboard];
        [weakself.passwordView startLoading];
        [[MYXHttpManager shared] cashMoneyWithMoneyCount:cashMoney
                                                 payname:@"alipay"
                                                   payid:payid
                                             paypassword:password
                                              withResult:^(BOOL sucess, NSString *msg) {
                                                  if (sucess) {
                                                      weakCell.allCashMoney = [NSString stringWithFormat:@"%.01f",weakself.restMoney.floatValue - cashMoney.floatValue];
                                                      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                          [SVProgressHUD showSuccessWithStatus:msg];
                                                          
                                                      });
                                                      
                                                  }else{
                                                      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                          ShowSVProgressErrorMessage(msg);
                                                         // [SVProgressHUD showErrorWithStatus:msg];
                                                          
                                                      });
                                                  }
                                                  [weakself.passwordView hide];
                                                  [[KGModal sharedInstance] hide];
                                                 
            
        }];

    };
}
- (void)cancel {
    CYLog(@"关闭密码框");
}

- (void)forgetPWD {
    CYLog(@"忘记密码");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
